//
//  FishPondPhoneView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/22.
//

#import "FishPondPhoneView.h"

@interface FishPondPhoneView ()
@property (nonatomic ,strong) UIView * phoneview;
@property (nonatomic ,strong) UILabel * contentLab;
@property (nonatomic ,strong) UIImageView * imgview;
@property (nonatomic ,strong) UIButton * deleteBtn;


@end

@implementation FishPondPhoneView
+ (instancetype)NewView{
    FishPondPhoneView * view = [FishPondPhoneView new];
    [view setupview];
    return view;
}
- (void)setupview{
    MJWeakSelf
    UIView * phoneview = [UIView new];
    self.phoneview = phoneview;
    [self addSubview:phoneview];
    phoneview.frame = CGRectMake(0, kwt(8), kwt(99), kwt(73));
    phoneview.addClickBlock(^{
        [weakSelf addimgClick];
    });
    phoneview.backgroundColor = UIColor.whiteColor;
    // 创建CAShapeLayer用于绘制虚线边框
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = phoneview.bounds;
    
    // 使用UIBezierPath创建圆角矩形路径
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:phoneview.bounds cornerRadius:kwt(5)];
    
    // 设置形状层的路径
    shapeLayer.path = path.CGPath;
    
    // 设置虚线样式
    shapeLayer.strokeColor = KcolorHex(@"#3D5FFF").CGColor; // 边框颜色
    shapeLayer.lineWidth = 1; // 边框宽度
    shapeLayer.lineDashPattern = @[@3, @3]; // 定义虚线模式，这里是4个单位的线，接着4个单位的空
    
    // 设置填充颜色为空，因为我们只需要边框
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    
    // 添加到视图的layer上
    [phoneview.layer addSublayer:shapeLayer];
    
    
    UIImageView * tempimg = kimageView(@"add_photos");
    [self addSubview:tempimg];
    [tempimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.offset(kwt(14));
        make.width.offset(kwt(31));
        make.height.offset(kwt(29));
    }];
    
    UILabel * contentLab = [DTKTool labelWithText:@"还可添加" andFont:kFont(14) andTextColor:KcolorHex(@"#787A79")];
    self.contentLab = contentLab;
    [phoneview addSubview:contentLab];
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.offset(kwt(48));
    }];
    
    UIImageView * imgview = kimageView(@"");
    [DTKTool YJ:kwt(5) andView:imgview];
    self.imgview = imgview;
    imgview.addClickBlock(^{
        [weakSelf imgclick];
    });
    [self addSubview:imgview];
    [imgview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(phoneview);
    }];
    
    UIButton * deleteBtn= [UIButton buttonWithType:0];
    self.deleteBtn = deleteBtn;
    deleteBtn.addimg([UIImage imageNamed:@"delete_photo"]).addTarget(self,@selector(BtnClick));
    [self addSubview:deleteBtn];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.offset(0);
        make.width.height.offset(kwt(23));
    }];
    [self deleteImg];
}
- (void)addimgClick{
    if(self.addImgBlock){
        self.addImgBlock();
    }
}
- (void)BtnClick{
    if(self.deleteBlock){
        self.deleteBlock();
    }
}
- (void)imgclick{
    if(self.openImgBlock){
        self.openImgBlock();
    }
}
///清空数据默认状态
- (void)deleteImg{
    self.deleteBtn.hidden = YES;
    self.imgview.hidden = YES;
    
}

- (void)setImgUrl:(NSString *)url{
    [self.imgview LZsetImageWithImagePath:url placeholderImage:[UIImage imageNamed:@"news_photos2"]];
    self.phoneview.hidden = YES;
    self.deleteBtn.hidden = NO;
    self.imgview.hidden = NO;
}

- (void)setImg:(UIImage *)img{
    self.imgview.image = img;
    self.deleteBtn.hidden = NO;
    self.imgview.hidden = NO;
}

- (void)setContent:(NSString *)content{
    self.contentLab.text = content;
}


@end
